﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Management;

namespace DemoApplication1
{
    // some of this files has been copied from http://www.codeproject.com/KB/cs/my_explorer.aspx 
    // and the forums pages below
    public partial class DbfSamples : Form
    {
        public DbfSamples()
        {
            InitializeComponent();
            var fi = new System.IO.FileInfo(Application.ExecutablePath);
            FillTree(tvFolders.Nodes, fi.Directory);
            tvFolders.ExpandAll();
        }

        private bool FillTree(TreeNodeCollection treeNodeCollection, System.IO.DirectoryInfo directoryInfo)
        {
            bool seenSomething = false;
            foreach (System.IO.DirectoryInfo di in directoryInfo.GetDirectories())
            {
                if (FillTree(treeNodeCollection, di)) seenSomething = true;
            }
            foreach (System.IO.FileInfo fi in directoryInfo.GetFiles())
            {
                switch (fi.Extension.ToLower())
                {
                    case ".dbf":
                        treeNodeCollection.Add(new FileInfoNode(fi));
                        seenSomething = true;
                        break;
                }
            }
            return seenSomething;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void tvFolders_AfterSelect(object sender, TreeViewEventArgs e)
        {
            var finode = e.Node as FileInfoNode;
            if (finode!=null)
            {
                dbfTableView1.DbfTable = new DbfDotNet.DbfTable(finode.FileInfo.FullName, System.Text.Encoding.UTF8, DbfDotNet.DbfVersion.dBaseIV);
            }
        }

 
    }
}
